package online.westonline.com.westonline.adapter.im;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;




import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Executor;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.DownloadCompletionCallback;
import cn.jpush.im.android.api.callback.ProgressUpdateCallback;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.PromptContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ContentType;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.enums.MessageStatus;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.android.api.options.MessageSendingOptions;
import cn.jpush.im.api.BasicCallback;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.utils.UiUtlis;
import online.westonline.com.westonline.utils.Utils;

/**
 * 聊天视图
 * 各种类型
 * Created by xiatao on 2018/7/16.
 */

public class ChatListAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //文本
    private final int TYPE_SEND_TXT = 0;
    private final int TYPE_RECEIVE_TXT = 1;
    // 图片
    private final int TYPE_SEND_IMAGE = 2;
    private final int TYPE_RECEIVER_IMAGE = 3;
    //文件
    private final int TYPE_SEND_FILE = 4;
    private final int TYPE_RECEIVE_FILE = 5;
    // 语音
    private final int TYPE_SEND_VOICE = 6;
    private final int TYPE_RECEIVER_VOICE = 7;
    // 位置
    private final int TYPE_SEND_LOCATION = 8;
    private final int TYPE_RECEIVER_LOCATION = 9;
    //群成员变动）
    private final int TYPE_GROUP_CHANGE = 10;
    //视频
    private final int TYPE_SEND_VIDEO = 11;
    private final int TYPE_RECEIVE_VIDEO = 12;
    //自定义消息
    private final int TYPE_CUSTOM_TXT = 13;
    //消息的各种状态
    private final int MESSAGE_SUCCESS=20;
    private final int MESSAGE_FALID=21;
    private final int MESSAGE_GOING=22;
    private final int MESSAGE_CREATE=23;
    //list_refresh
    private final int REFRESH_LIST=100;
    private final Conversation conversation;
    private Executor executor;
    //多图上传
    private Handler myhander = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(android.os.Message msg) {
            if (msg.what==REFRESH_LIST)
            {
               notifyItemChanged(messageList.size()-1);
            }
            return true;
        }
    });
    //图片上传线程
    //发送图片消息的队列
    private Queue<Message> mMsgQueue = new LinkedList<Message>();
    //各种事件
    private onImageClickListener imageClickListener;
    private onFileClickListener fileClickListener;
    private onVoiceClickListener voiceClickListener;

    private List<Message> messageList = new ArrayList<>();
    //消息发送状态
    private boolean success = true;
    //某个播放item状态
    private int item_play_time;
    private int select_record_position;
    private int select_msg_id;
    private int state;
    private int start_pos;

    public ChatListAdpter(Conversation conversation)
    {
        this.conversation = conversation;

    }
    public void setMessageList(List<Message> messageList) {
        this.messageList = messageList;
    }

    public void setFileClickListener(onFileClickListener fileClickListener) {
        this.fileClickListener = fileClickListener;
    }

    public void setImageClickListener(onImageClickListener imageClickListener) {
        this.imageClickListener = imageClickListener;
    }

    public void setVoiceClickListener(onVoiceClickListener voiceClickListener) {
        this.voiceClickListener = voiceClickListener;
    }

    /**
     * 格式化显示playtime
     *
     * @param play_time
     * @return
     */
    private String playTimeFormat(int play_time) {
        String result = null;
        if (play_time < 10) {
            result = "00" + ":0" + play_time;

        }
        if (play_time > 10) {
            result = "00:" + play_time;
            if (play_time == 60) {
                result = "01:00";
            }
            if (play_time > 60) {
                int minute = play_time / 60;
                int scend = play_time % 60;
                if (minute < 10) {
                    if (scend < 10) {
                        result = "0" + minute + ":" + "0" + scend;
                    }
                    if (scend > 10) {
                        result = "0" + minute + ":" + scend;
                    }
                }
                if (minute > 10) {
                    if (scend < 10) {
                        result = minute + ":" + "0" + scend;
                    }
                    if (scend > 10) {
                        result = minute + ":" + scend;
                    }
                }
            }
        }
        return result;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = null;
        //文本
        if (viewType == TYPE_SEND_TXT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_send_text, parent, false);
            return new MySendTextViewHolder(view);
        } else if (viewType == TYPE_RECEIVE_TXT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_recivice_text, parent, false);
            return new MyReciviceTextViewHolder(view);
            //声音
        } else if (viewType == TYPE_SEND_VOICE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_voice_send, parent, false);
            return new MySendVoiceViewHolder(view);
        } else if (viewType == TYPE_RECEIVER_VOICE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_voice_recivice, parent, false);
            return new MyReciviceVoiceViewHolder(view);
            //图片
        } else if (viewType == TYPE_SEND_IMAGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_send_img, parent, false);
            return new MySendImgViewHolder(view);
        } else if (viewType == TYPE_RECEIVER_IMAGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_recivice_img, parent, false);
            return new MyReciviceImgViewHolder(view);
        }
        //消息相关
        else if (viewType == TYPE_GROUP_CHANGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_notifiy_message, parent, false);
            return new MyNotifyViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Message one_message = messageList.get(position);
        //屏幕的宽度(px值）
        int screenWidth = holder.itemView.getContext().getResources().getDisplayMetrics().widthPixels;
        //Item的宽度，或图片的宽度
        int width = screenWidth/2;
        //自己发送文本
        if (holder instanceof MySendTextViewHolder) {
            final MySendTextViewHolder sendTextViewHolder = (MySendTextViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            TextContent textContent = (TextContent) one_message.getContent();
            String nickname = userInfo.getNickname();
            //消息状态判断
            //1为成功或者是之前的
            //0为失败
            //此为静态状态,初始化时该做的
            updateStauts(one_message,sendTextViewHolder);
            if (nickname.length() > 1) {
                sendTextViewHolder.tvSendTextName.setText(nickname.substring(0, 1));
            }
            sendTextViewHolder.tvSendTextContent.setText(textContent.getText());
        }
        //收到的文本
        if (holder instanceof MyReciviceTextViewHolder) {
            MyReciviceTextViewHolder reciviceTextViewHolder = (MyReciviceTextViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            TextContent textContent = (TextContent) one_message.getContent();
            String nickname = userInfo.getNickname();
            reciviceTextViewHolder.tvRecviceTextName.setText(nickname);
            if (one_message.haveRead()) {
                reciviceTextViewHolder.tvRecviceTextState.setText("已读");
                reciviceTextViewHolder.tvRecviceTextState.setTextColor(UiUtlis.getColor(R.color.Event_Green));
            }
            if (nickname.length() > 1) {
                reciviceTextViewHolder.tvRecviceTextName.setText(nickname.substring(0, 1));
            }
            reciviceTextViewHolder.tvRecviceTextContent.setText(textContent.getText());
        }
        //自己发送的语音
        if (holder instanceof MySendVoiceViewHolder) {
            MySendVoiceViewHolder voiceViewHolder = (MySendVoiceViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            String nickname = userInfo.getNickname();
            VoiceContent voiceContent = (VoiceContent) one_message.getContent();
            String format_time = playTimeFormat(voiceContent.getDuration());
            updateStauts(one_message,voiceViewHolder);
            if (select_msg_id != 0 && select_msg_id == one_message.getId()) {
                //被选中了
                format_time = playTimeFormat(item_play_time);
            }
            voiceViewHolder.tvSendVoicePlaytime.setText(format_time);
            voiceViewHolder.tvSendVoiceName.setText(nickname);
            if (nickname.length() > 1) {
                voiceViewHolder.tvSendVoiceName.setText(nickname.substring(0, 1));
            }
        }
        //收到的语音
        if (holder instanceof MyReciviceVoiceViewHolder) {
            MyReciviceVoiceViewHolder rvoiceViewHolder = (MyReciviceVoiceViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            String nickname = userInfo.getNickname();
            VoiceContent voiceContent = (VoiceContent) one_message.getContent();
            String format_time = playTimeFormat(voiceContent.getDuration());
            if (select_msg_id != 0 && select_msg_id == one_message.getId()) {
                //被选中了
                format_time = playTimeFormat(item_play_time);
            }
            rvoiceViewHolder.tvReciviceVoicePlaytime.setText(format_time);
            rvoiceViewHolder.tvReciviceVoiceName.setText(nickname);

            if (nickname.length() > 1) {
                rvoiceViewHolder.tvReciviceVoiceName.setText(nickname.substring(0, 1));
            }
        }
        //自己发送的图片
        if (holder instanceof MySendImgViewHolder) {
            final MySendImgViewHolder imgViewHolder = (MySendImgViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            String nickname = userInfo.getNickname();
            ImageContent imageContent = (ImageContent) one_message.getContent();
            //先设置宽高(最小宽高)
            imgViewHolder.imgSendContent.setMaxHeight(Utils.dp2px(imageContent.getHeight()));
            imgViewHolder.imgSendContent.setMaxWidth(Utils.dp2px(imageContent.getWidth()));
            //自己发的在本地里(不要glide)(拿略缩图)
            imageContent.downloadThumbnailImage(one_message, new DownloadCompletionCallback() {
                @Override
                public void onComplete(int i, String s, File file) {
                    //拿到文件
                    if (i==0)
                    {
                        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
                        imgViewHolder.imgSendContent.setImageBitmap(bitmap);
                    }
                }

            });
            updateStauts(one_message,imgViewHolder);
            imgViewHolder.tvSendImgName.setText(nickname);
            if (nickname.length() > 1) {
                imgViewHolder.tvSendImgName.setText(nickname.substring(0, 1));
            }
        }
        //接收别人的图片
        if (holder instanceof MyReciviceImgViewHolder) {
            final MyReciviceImgViewHolder imgViewHolder = (MyReciviceImgViewHolder) holder;
            UserInfo userInfo = one_message.getFromUser();
            String nickname = userInfo.getNickname();
            ImageContent imageContent = (ImageContent) one_message.getContent();
            //先设置宽高(最小宽高)
            imgViewHolder.imgReciviceContent.setMinimumHeight(Utils.dp2px(imageContent.getHeight()));
            imgViewHolder.imgReciviceContent.setMinimumWidth(Utils.dp2px(imageContent.getWidth()));
            if (one_message.haveRead()) {
                imgViewHolder.tvRecviceImgState.setText("已读");
                imgViewHolder.tvRecviceImgState.setTextColor(UiUtlis.getColor(R.color.Event_Green));
            }
            if (nickname.length() > 1) {
                imgViewHolder.tvReciviceImgName.setText(nickname.substring(0, 1));
            }
            //去下载(缩略图)
            imageContent.downloadThumbnailImage(one_message, new DownloadCompletionCallback() {
                @Override
                public void onComplete(int i, String s, File file) {
                    if (i == 0) {
                        //表示下载完成
                        imgViewHolder.proReciviceImgState.setVisibility(View.GONE);
                        //去加载(按动图加载)
                        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
                        imgViewHolder.imgReciviceContent.setImageBitmap(bitmap);
                    }
                }
            });
            imgViewHolder.tvReciviceImgName.setText(nickname);
            if (nickname.length() > 1) {
                imgViewHolder.tvReciviceImgName.setText(nickname.substring(0, 1));
            }
        }
        //消息
        if (holder instanceof MyNotifyViewHolder)
        {
            MyNotifyViewHolder notifyViewHolder = (MyNotifyViewHolder) holder;
            PromptContent promptContent = (PromptContent) one_message.getContent();
            //先把文本设置上去
            notifyViewHolder.tvNotifyMessage.setText(promptContent.getPromptText());
            switch (promptContent.getPromptType())
            {
                case message_retract:
                    notifyViewHolder.tvNotifyMessage.setText(promptContent.getPromptText());
                    break;
            }
        }
    }

    /**
     * 更新消息发送的状态
     * @param one_message
     */
    private void updateStauts(Message one_message,RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof MySendTextViewHolder)
        {
            MySendTextViewHolder sendTextViewHolder = (MySendTextViewHolder) viewHolder;
            if (getMessageState(one_message)==MESSAGE_SUCCESS)
            {
                sendTextViewHolder.imgFlaidState.setVisibility(View.GONE);
                sendTextViewHolder.proSendState.setVisibility(View.GONE);
            }
            if (getMessageState(one_message)==MESSAGE_FALID)
            {
                sendTextViewHolder.proSendState.setVisibility(View.GONE);
                sendTextViewHolder.imgFlaidState.setVisibility(View.VISIBLE);
            }
            if (getMessageState(one_message)==MESSAGE_GOING)
            {
                sendTextViewHolder.proSendState.setVisibility(View.VISIBLE);
                sendTextViewHolder.imgFlaidState.setVisibility(View.GONE);
            }
        }
        if (viewHolder instanceof MySendVoiceViewHolder)
        {
            MySendVoiceViewHolder voiceViewHolder = (MySendVoiceViewHolder) viewHolder;
            if (getMessageState(one_message)==MESSAGE_SUCCESS)
            {
                voiceViewHolder.imgFlaidState.setVisibility(View.GONE);
                voiceViewHolder.proSendState.setVisibility(View.GONE);
            }
            if (getMessageState(one_message)==MESSAGE_FALID)
            {
                voiceViewHolder.proSendState.setVisibility(View.GONE);
                voiceViewHolder.imgFlaidState.setVisibility(View.VISIBLE);
            }
            if (getMessageState(one_message)==MESSAGE_GOING)
            {
                voiceViewHolder.proSendState.setVisibility(View.VISIBLE);
                voiceViewHolder.imgFlaidState.setVisibility(View.GONE);
            }
        }
        if (viewHolder instanceof MySendImgViewHolder)
        {
            MySendImgViewHolder imgViewHolder = (MySendImgViewHolder) viewHolder;
            if (getMessageState(one_message)==MESSAGE_SUCCESS)
            {
                imgViewHolder.imgFlaidState.setVisibility(View.GONE);
                imgViewHolder.proSendImgState.setVisibility(View.GONE);
            }
            if (getMessageState(one_message)==MESSAGE_FALID)
            {
                imgViewHolder.proSendImgState.setVisibility(View.GONE);
                imgViewHolder.imgFlaidState.setVisibility(View.VISIBLE);
            }
            if (getMessageState(one_message)==MESSAGE_GOING)
            {
                imgViewHolder.proSendImgState.setVisibility(View.VISIBLE);
                imgViewHolder.imgFlaidState.setVisibility(View.GONE);
            }
        }

    }

    /**
     * 获取消息的发送状态
     * @param one_message
     * @return
     */
    private int getMessageState(Message one_message) {
        switch (one_message.getStatus())
        {
            case created:
                return MESSAGE_GOING;
            case send_fail:
                return MESSAGE_FALID;
            case send_draft:

            case send_going:
                return MESSAGE_GOING;
            case receive_fail:
                return MESSAGE_FALID;
            case send_success:
                return MESSAGE_SUCCESS;
            case receive_going:
                return MESSAGE_GOING;
            case receive_success:
                //收到成功也算
                return MESSAGE_SUCCESS;
            default:
                return MESSAGE_FALID;
        }
    }

    @Override
    public int getItemCount() {
        return messageList == null ? 0 : messageList.size();
    }

    @Override
    public int getItemViewType(int position) {
        //获取数据源
        Message message = messageList.get(position);
        //判断是否是
        switch (message.getContentType()) {
            case text:
                return message.getDirect() == MessageDirect.send ? TYPE_SEND_TXT
                        : TYPE_RECEIVE_TXT;
            case image:
                return message.getDirect() == MessageDirect.send ? TYPE_SEND_IMAGE
                        : TYPE_RECEIVER_IMAGE;
            case file:
                String extra = message.getContent().getStringExtra("video");
                if (!TextUtils.isEmpty(extra)) {
                    return message.getDirect() == MessageDirect.send ? TYPE_SEND_VIDEO
                            : TYPE_RECEIVE_VIDEO;
                } else {
                    return message.getDirect() == MessageDirect.send ? TYPE_SEND_FILE
                            : TYPE_RECEIVE_FILE;
                }
            case voice:
                return message.getDirect() == MessageDirect.send ? TYPE_SEND_VOICE
                        : TYPE_RECEIVER_VOICE;
            case location:
                return message.getDirect() == MessageDirect.send ? TYPE_SEND_LOCATION
                        : TYPE_RECEIVER_LOCATION;
            case eventNotification:
            case prompt:
                return TYPE_GROUP_CHANGE;
            default:
                return TYPE_CUSTOM_TXT;
        }
    }

    /**
     * 已读
     *
     * @param serverMsgId
     * @param unReceiptCnt
     */
    public void setUpdateReceiptCount(long serverMsgId, int unReceiptCnt) {
        for (Message message : messageList) {
            if (message.getServerMessageId() == serverMsgId) {
                message.setUnreceiptCnt(unReceiptCnt);
            }
        }
        notifyItemRangeChanged(0,messageList.size()-1);
    }

    /***
     * 消息发送状态
     * @param flinalpos
     * @param state 状态
     */
    public void sending(int flinalpos, int state) {
        this.state = state;
        notifyItemChanged(flinalpos);
    }


    /**
     * 自己发送文本的布局
     */
    class MySendTextViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_send_time)
        TextView tvSendTime;
        @BindView(R.id.img_flaid_state)
        ImageView imgFlaidState;
        @BindView(R.id.pro_send_state)
        ProgressBar proSendState;
        @BindView(R.id.tv_send_text_state)
        TextView tvSendTextState;
        @BindView(R.id.tv_send_text_content)
        TextView tvSendTextContent;
        @BindView(R.id.tv_send_text_name)
        TextView tvSendTextName;

        public MySendTextViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 接收别人文本的布局
     */
    class MyReciviceTextViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recvice_time)
        TextView tvRecviceTime;
        @BindView(R.id.tv_recvice_text_name)
        TextView tvRecviceTextName;
        @BindView(R.id.tv_recvice_text_content)
        TextView tvRecviceTextContent;
        @BindView(R.id.tv_recvice_text_state)
        TextView tvRecviceTextState;

        public MyReciviceTextViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 自己发送的语音布局
     */
    class MySendVoiceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_send_voice_time)
        TextView tvSendVoiceTime;
        @BindView(R.id.img_flaid_state)
        ImageView imgFlaidState;
        @BindView(R.id.pro_send_state)
        ProgressBar proSendState;
        @BindView(R.id.tv_send_voice_state)
        TextView tvSendVoiceState;
        @BindView(R.id.img_chat_voice_state)
        ImageView imgChatVoiceState;
        @BindView(R.id.tv_send_voice_playtime)
        TextView tvSendVoicePlaytime;
        @BindView(R.id.tv_send_voice_name)
        TextView tvSendVoiceName;

        public MySendVoiceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != voiceClickListener) {
                        VoiceContent voiceContent = (VoiceContent) messageList.get(getLayoutPosition()).getContent();
                        int time = voiceContent.getDuration();
                        String path = voiceContent.getLocalPath();
                        voiceClickListener.VoiceClick(getLayoutPosition(), path, time);
                    }
                }
            });
        }
    }

    /**
     * 接收别人的语音布局
     */
    class MyReciviceVoiceViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recivice_voice_time)
        TextView tvReciviceVoiceTime;
        @BindView(R.id.tv_recivice_voice_name)
        TextView tvReciviceVoiceName;
        @BindView(R.id.tv_recivice_voice_playtime)
        TextView tvReciviceVoicePlaytime;
        @BindView(R.id.img_chat_voice_state)
        ImageView imgChatVoiceState;

        public MyReciviceVoiceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != voiceClickListener) {
                        VoiceContent voiceContent = (VoiceContent) messageList.get(getLayoutPosition()).getContent();
                        int time = voiceContent.getDuration();
                        String path = voiceContent.getLocalPath();
                        voiceClickListener.VoiceClick(getLayoutPosition(), path, time);
                    }
                }
            });
        }
    }

    /**
     * 自己发送的图片布局
     */
    class MySendImgViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_send_img_time)
        TextView tvSendImgTime;
        @BindView(R.id.img_flaid_state)
        ImageView imgFlaidState;
        @BindView(R.id.pro_send_img_state)
        ProgressBar proSendImgState;
        @BindView(R.id.tv_send_img_state)
        TextView tvSendImgState;
        @BindView(R.id.img_send_content)
        ImageView imgSendContent;
        @BindView(R.id.tv_send_img_name)
        TextView tvSendImgName;

        public MySendImgViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != imageClickListener) {
                        ImageContent content = (ImageContent) messageList.get(getLayoutPosition()).getContent();
                        imageClickListener.ImageClick(getLayoutPosition(), content.getLocalPath());
                    }
                }
            });
        }
    }

    /**
     * 接收别人的图片布局
     */
    class MyReciviceImgViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pro_recivice_img_state)
        ProgressBar proReciviceImgState;
        @BindView(R.id.tv_recivice_img_time)
        TextView tvReciviceImgTime;
        @BindView(R.id.tv_recivice_img_name)
        TextView tvReciviceImgName;
        @BindView(R.id.img_recivice_content)
        ImageView imgReciviceContent;
        @BindView(R.id.tv_recvice_img_state)
        TextView tvRecviceImgState;

        public MyReciviceImgViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != imageClickListener) {
                        ImageContent content = (ImageContent) messageList.get(getLayoutPosition()).getContent();
                        imageClickListener.ImageClick(getLayoutPosition(), content.getLocalThumbnailPath());
                    }
                }
            });
        }
    }

    /**
     * 通知布局
     */
    class MyNotifyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_notify_message)
        TextView tvNotifyMessage;
        public MyNotifyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 更新某个录音item
     *
     * @param id
     * @param record_time
     * @param now_click_pos
     */
    public void playItem(int id, int record_time, int now_click_pos) {
        item_play_time = record_time;
        select_record_position = now_click_pos;
        select_msg_id = id;
        notifyItemChanged(now_click_pos);
    }
    //其消息的发送和加载都到这个界面来

    /**
     * 发送文本消息
     * @param text
     * @param lvChat
     */
    public void updateTextMsg(TextContent text, RecyclerView lvChat)
    {
        //消息发送状态已经做好,只需再这里发送随后更新内容
        Message message = conversation.createSendMessage(text);
        MessageSendingOptions options = new MessageSendingOptions();
        options.setNeedReadReceipt(true);
        //先加入集合
        messageList.add(message);
        message.setOnSendCompleteCallback(new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                //只需更新
                notifyItemChanged(messageList.size()-1);
            }
        });
        JMessageClient.sendMessage(message,options);
        lvChat.scrollToPosition(messageList.size()-1);
        //更新
        notifyItemChanged(messageList.size()-1);
    }

    /***
     * 更新语音消息
     * @param voiceContent
     * @param lvChat
     */
    public void updateVoiceMsg(VoiceContent voiceContent,RecyclerView lvChat)
    {
        final Message message = conversation.createSendMessage(voiceContent);
        MessageSendingOptions options = new MessageSendingOptions();
        options.setNeedReadReceipt(true);
        messageList.add(message);
        message.setOnContentUploadProgressCallback(new ProgressUpdateCallback() {
            @Override
            public void onProgressUpdate(double v) {
                if (v==1.0)
                {
                    //上传完成
                    notifyItemChanged(messageList.size()-1);
                    //更新一下发送状态
                }
            }
        });
        JMessageClient.sendMessage(message,options);
        lvChat.scrollToPosition(messageList.size()-1);
        //更新
        notifyItemChanged(messageList.size()-1);
    }

    /**
     * 检查图片是否处于创建状态，如果是，则加入发送队列
     *
     */
    public void checkSendingImgMsg(int start_pos)
    {
        for (Message msg : messageList) {
            if (msg.getStatus() == MessageStatus.created
                    && msg.getContentType() == ContentType.image) {
                mMsgQueue.offer(msg);
            }
        }
        if (mMsgQueue.size() > 0) {
            Message message = mMsgQueue.element();
            if (conversation.getType() == ConversationType.single) {
                sendNextImgMsg(message);
            } else {
                sendNextImgMsg(message);
            }
         //   notifyItemRangeChanged(0,messageList.size()-1);
        }
    }
    /**
     * 从发送队列中出列，并发送图片
     *
     * @param msg 图片消息
     */
    private void sendNextImgMsg(final Message msg) {
        MessageSendingOptions options = new MessageSendingOptions();
        options.setNeedReadReceipt(true);
        JMessageClient.sendMessage(msg, options);
        msg.setOnContentUploadProgressCallback(new ProgressUpdateCallback() {
            @Override
            public void onProgressUpdate(double v) {
                //获取该msg的位置
                for (int i1=0;i1<messageList.size();i1++)
                {
                    if (messageList.get(i1).getServerMessageId()==msg.getServerMessageId())
                    {
                        //固定位置更新
                        notifyItemRangeChanged(i1,messageList.size());
                    }
                }
            }
        });
        msg.setOnSendCompleteCallback(new BasicCallback() {
            @Override
            public void gotResult(int i, String s) {
                //出列
                mMsgQueue.poll();
                //如果队列不为空，则继续发送下一张
                if (!mMsgQueue.isEmpty()) {
                    sendNextImgMsg(mMsgQueue.element());
                }
            }
        });
    }

    /**
     * 图片点击事件
     */
    public interface onImageClickListener {
        void ImageClick(int pos, String path);
    }

    /**
     * 语音点击事件
     */
    public interface onVoiceClickListener {
        void VoiceClick(int pos, String path, int durction);
    }

    /**
     * 文件点击事件
     */
    public interface onFileClickListener {
        void FlieClick(int pos, String path);
    }


}
